<?php
/**
 * @name __autoLoad()
 * @desc 类的自动加载函数,遵循zend framework类的命名规范
 * @param String $className 类名
 */
function __autoLoad($className)
{
	define("PROJECT_DIR", "PHPSeniorDev");
	$parts = explode('_', $className);
	$path = $_SERVER['DOCUMENT_ROOT'] . '/' . PROJECT_DIR . "/" . implode("/", $parts);
	require_once $path . '.php';
}

//生成模板对象
$tpl = new Lab8_MyTemplate();

//连接数据库
require_once './mysql_connect.inc.php';

//设置每页显示留言个数
define("PAGE_SIZE", 10);

//取得总共的页数
$countSql = "SELECT * FROM `msg`";
$countRes = mysql_query($countSql);
$count = mysql_num_rows($countRes);
$totalPage = ceil($count/PAGE_SIZE);

//清空数据库结果资源
mysql_free_result($countRes);

//设置当前页码
$currentPage = isset($_GET['page'])?$_GET['page']:1;
if ($currentPage < 1 || $currentPage > $totalPage) $currentPage = 1;

//当前页的开始记录序号
$pageBegin = ($currentPage-1) * PAGE_SIZE;

//获取数据库的记录
$size = PAGE_SIZE;
$msgSql = "SELECT `msg`.*,`user`.`image`,`user`.`name` AS author FROM `msg`,`user` 
           WHERE `msg`.`authorid` = `user`.`id` 
           ORDER BY `time` DESC 
           LIMIT {$pageBegin},{$size}";
$msgRes = mysql_query($msgSql);

//从结果集中查询数据，生成二维数组
$msgs = array();
if ($msgRes != false) {
	while ($msg = mysql_fetch_assoc($msgRes)) {
		$msgs[] = $msg;
	}
}

//为模板变量赋值
$tpl->msgs = $msgs;
$tpl->currentPage = $currentPage;
$tpl->totalpage = $totalPage;

//显示视图文件
$template = '';
if (isset($_GET['tpl']) && $_GET['tpl']!='') {
	$template = $_GET['tpl'] . '.phtml';
}

$tpl->display($template);

